package com.qiankun.tang.ui.view.adapter.find;

import android.content.Context;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.common.library.listener.OnAppListener;
import com.common.library.ui.view.adapter.base.BaseRecyclerAdapter;
import com.facebook.drawee.view.SimpleDraweeView;
import com.qiankun.tang.R;
import com.qiankun.tang.beans.AuthorBean;
import com.qiankun.tang.beans.VideoSynopsisBean;
import com.qiankun.tang.beans.items.AuthorItem;
import com.qiankun.tang.ui.view.SpecialSpacingItemDecoration;
import com.qiankun.tang.ui.view.VideoSynopsisListWidget;

/**
 * @author txf
 * @Title
 * @package com.qiankun.video.ui.view.adapter
 * @date 2017/6/13 0013
 */

public class AuthorAdapter extends BaseRecyclerAdapter<AuthorItem> {
    public AuthorAdapter(Context context) {
        super(context);
    }

    public AuthorAdapter(Context context, OnAppListener.OnInteractionListener listener) {
        super(context, listener);
    }
    @Override
    public int getItemViewType(int position) {
        return getDatas().get(position).getType();
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        switch (viewType) {
            case AuthorItem.TYPE_0:
                return new AuthorViewHolder0(LayoutInflater.from(getContext()).inflate(R.layout.item_author_0, parent, false));
            case AuthorItem.TYPE_1:
                return new AuthorViewHolder1(LayoutInflater.from(getContext()).inflate(R.layout.item_author_1, parent, false));
            case AuthorItem.TYPE_2:
                return new AuthorViewHolder2(LayoutInflater.from(getContext()).inflate(R.layout.item_author_2, parent, false));
        }
        return null;
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        if (holder instanceof AuthorViewHolder0) {

            onBindAuthorViewHolder0((AuthorViewHolder0) holder, position);

        } else if (holder instanceof AuthorViewHolder1) {

            onBindAuthorViewHolder1((AuthorViewHolder1) holder, position);

        } else if (holder instanceof AuthorViewHolder2) {

            onBindAuthorViewHolder2((AuthorViewHolder2) holder, position);

        }
    }

    private void onBindAuthorViewHolder0(AuthorViewHolder0 holder, int position) {
        String data = (String) getDatas().get(position).getBean();
        holder.mTv.setText(data);
    }

    private void onBindAuthorViewHolder1(AuthorViewHolder1 holder, int position) {
        final AuthorBean bean = (AuthorBean) getDatas().get(position).getBean();
        holder.mIcon.setImageURI(bean.getIconUrl());
        holder.title.setText(bean.getTitle());
        holder.subtitle.setText(bean.getSubtitle());
        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                getListener().onInteraction(AuthorItem.TYPE_1,bean);
            }
        });
    }

    private void onBindAuthorViewHolder2(AuthorViewHolder2 holder, int position) {
        final AuthorBean bean = (AuthorBean) getDatas().get(position).getBean();
        holder.mIcon.setImageURI(bean.getIconUrl());
        holder.title.setText(bean.getTitle());
        holder.subtitle.setText(bean.getSubtitle());
        holder.mVideoSynopsisListWidget.setData(bean.getList());
        holder.mVideoSynopsisListWidget.addOnItemClickListener(new VideoSynopsisListWidget.OnItemClickListener() {
            @Override
            public void onItemClick(int type, VideoSynopsisBean bean) {
                getListener().onInteraction(AuthorItem.TYPE_2,bean);
            }
        });
    }

    class AuthorViewHolder0 extends RecyclerView.ViewHolder {
        TextView mTv;

        public AuthorViewHolder0(View itemView) {
            super(itemView);
            mTv = (TextView) itemView.findViewById(R.id.item_author_0_tv);
        }
    }
    class AuthorViewHolder1 extends RecyclerView.ViewHolder {
        SimpleDraweeView mIcon;
        TextView follow,title,subtitle;
        public AuthorViewHolder1(View itemView) {
            super(itemView);
            mIcon = (SimpleDraweeView) itemView.findViewById(R.id.item_hot_1_icon);
            follow = (TextView) itemView.findViewById(R.id.item_hot_1_follow);
            title = (TextView) itemView.findViewById(R.id.item_hot_1_title);
            subtitle = (TextView) itemView.findViewById(R.id.item_hot_1_subtitle);
        }
    }
    class AuthorViewHolder2 extends RecyclerView.ViewHolder {
        VideoSynopsisListWidget mVideoSynopsisListWidget;
        SimpleDraweeView mIcon;
        TextView follow,title,subtitle;
        public AuthorViewHolder2(View itemView) {
            super(itemView);
            itemView.findViewById(R.id.item_hot_1_line).setVisibility(View.GONE);
            itemView.findViewById(R.id.item_hot_1_null).setVisibility(View.GONE);

            mIcon = (SimpleDraweeView) itemView.findViewById(R.id.item_hot_1_icon);
            follow = (TextView) itemView.findViewById(R.id.item_hot_1_follow);
            title = (TextView) itemView.findViewById(R.id.item_hot_1_title);
            subtitle = (TextView) itemView.findViewById(R.id.item_hot_1_subtitle);

            mVideoSynopsisListWidget = (VideoSynopsisListWidget) itemView.findViewById(R.id.item_author_2_VideoSynopsisLisetWidget);
            mVideoSynopsisListWidget.setItemDecoration(new SpecialSpacingItemDecoration(LinearLayoutManager.HORIZONTAL,dip2px(15),dip2px(4),true));
        }
    }
}
